package server.io.client;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;

public class Send_Module
{
	public void send() throws ClientProtocolException, IOException, URISyntaxException
	{
		HttpClient httpclient = new DefaultHttpClient();

		URIBuilder builder = new URIBuilder();
		builder.setScheme("http").setHost("192.168.1.200").setPath("/kado/inter/login.php")
		    .setParameter("q", "httpclient")
		    .setParameter("btnG", "Google Search")
		    .setParameter("aq", "f")
		    .setParameter("oq", "");
		URI uri = builder.build();
		
		
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("param1", "value1"));
		formparams.add(new BasicNameValuePair("param2", "value2"));
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
		HttpPost httppost = new HttpPost(uri);
		System.out.println(httppost.getURI());
		httppost.setEntity(entity);
		
		HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
				HttpStatus.SC_OK, "OK");

		response = httpclient.execute(httppost);
		HttpEntity entity1 = response.getEntity();
		if (entity1 != null) {
		    InputStream instream = entity1.getContent();
		    byte[] d = new byte[4096];
		    System.out.println("::" + instream.read(d));
		    String res = new String(d);
		    System.out.println(":::" + res);
		    
		    try {
		        // do something useful
		    } finally {
		        instream.close();
		    }
		}
		
		System.out.println(response.getAllHeaders()[3]);
		
		for(int i = 0; i < response.getAllHeaders().length; i++)
		{
			System.out.println(response.getAllHeaders()[i]);
		}
		
	}
}
